<template>
  <div class="app-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>套餐管理</span>
          <el-button type="primary" @click="handleAdd">新增套餐</el-button>
        </div>
      </template>

      <!-- 搜索区域 -->
      <el-form :model="queryParams" ref="queryForm" :inline="true">
        <el-form-item label="套餐名称" prop="name">
          <el-input v-model="queryParams.name" placeholder="请输入套餐名称" clearable style="width: 200px" />
        </el-form-item>
        <el-form-item label="套餐类型" prop="type">
          <el-input v-model="queryParams.type" placeholder="请输入套餐类型" clearable style="width: 200px" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">搜索</el-button>
          <el-button @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <!-- 数据表格 -->
      <el-table v-loading="loading" :data="packageList">
        <el-table-column label="套餐ID" prop="id" width="80" />
        <el-table-column label="套餐名称" prop="name" />
        <el-table-column label="套餐类型" prop="type" />
        <el-table-column label="套餐描述" prop="decription" show-overflow-tooltip />
        <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button type="primary" link @click="handleUpdate(scope.row)">修改</el-button>
            <el-button type="danger" link @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页组件 -->
      <pagination
        v-show="total > 0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />

      <!-- 添加或修改套餐对话框 -->
      <el-dialog :title="title" v-model="open" width="500px" append-to-body>
        <el-form ref="packageForm" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="套餐名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入套餐名称" />
          </el-form-item>
          <el-form-item label="套餐类型" prop="type">
            <el-input v-model="form.type" placeholder="请输入套餐类型" />
          </el-form-item>
          <el-form-item label="套餐描述" prop="decription">
            <el-input v-model="form.decription" type="textarea" placeholder="请输入套餐描述" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { listPackage, getPackage, delPackage, addPackage, updatePackage } from '@/api/app-store/package'

// 表单ref
const packageForm = ref(null)

// 遮罩层
const loading = ref(false)
// 总条数
const total = ref(0)
// 套餐表格数据
const packageList = ref([])
// 弹出层标题
const title = ref('')
// 是否显示弹出层
const open = ref(false)

// 查询参数
const queryParams = ref({
  pageNum: 1,
  pageSize: 10,
  name: undefined,
  type: undefined
})

// 表单参数
const form = ref({
  id: undefined,
  name: undefined,
  type: undefined,
  decription: undefined
})

// 表单校验
const rules = {
  name: [{ required: true, message: '套餐名称不能为空', trigger: 'blur' }],
  type: [{ required: true, message: '套餐类型不能为空', trigger: 'blur' }]
}

/** 查询套餐列表 */
function getList() {
  loading.value = true
  listPackage(queryParams.value).then(response => {
    packageList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

/** 表单重置 */
function reset() {
  form.value = {
    id: undefined,
    name: undefined,
    type: undefined,
    decription: undefined
  }
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value = {
    pageNum: 1,
    pageSize: 10,
    name: undefined,
    type: undefined
  }
  handleQuery()
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加套餐'
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const id = row.id || row
  getPackage(id).then(response => {
    form.value = response.data
    open.value = true
    title.value = '修改套餐'
  })
}

/** 提交按钮 */
function submitForm() {
  packageForm.value.validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        updatePackage(form.value).then(response => {
          ElMessage.success('修改成功')
          open.value = false
          getList()
        })
      } else {
        addPackage(form.value).then(response => {
          ElMessage.success('新增成功')
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const ids = row.id || row
  ElMessageBox.confirm('是否确认删除套餐编号为"' + ids + '"的数据项?', '警告', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(function() {
    return delPackage(ids)
  }).then(() => {
    getList()
    ElMessage.success('删除成功')
  })
}
getList()
</script>

<style scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.dialog-footer {
  text-align: center;
}
</style>